package cn.gz.zoomtech.common;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.log4j.Logger;

public class ConfManager {
	private static final Logger logger = Logger.getLogger(ConfManager.class);

	private CompositeConfiguration conf;
	private static ConfManager instance = null;

	/**
	 * 获取ConfManager实例
	 * @return ConfManager
	 */
	public static synchronized ConfManager getInstance(){
		if (instance == null) instance = new ConfManager();
		return instance;
	}
	
	private ConfManager(){
		String confFileName = "/selfconf.properties";
		try {
			ToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE); //ToString的样式
			conf = new CompositeConfiguration();
			
			String propsFileName = getClass().getResource(confFileName).getFile();
			
			PropertiesConfiguration propsConfig = new PropertiesConfiguration();
			propsConfig.load(propsFileName);
			FileChangedReloadingStrategy propsReload = new FileChangedReloadingStrategy();
//			每10分钟检查一次更新,此处单位是毫秒 ,1000毫秒=1S   
//			10分钟=10*1分钟=10*60*1S=10*60*1000毫秒=600000毫秒
			propsReload.setRefreshDelay(600000L); 
			propsConfig.setReloadingStrategy(propsReload);
			conf.addConfiguration(propsConfig);
		} catch (Exception ex) {
			logger.error("config file name:"+confFileName+",ConfigInitError:", ex);
			System.exit(-1);//退出程序
		}
	}

	/**
	 * 获取配置
	 * @return Properties
	 */
	public CompositeConfiguration getConf() {
		return conf;
	}
	
}
